<?php

namespace App\Http\Controllers\AdminShop\Shoper;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Shoper;
use Hash;
use DB;

class LoginController extends Controller
{
    /**
     * 商家后台登陆
     */
    public function login()
    {
        
        return view('adminShop.shoper.login');
    }

    /**
     * 执行登陆
     */
    public function doLogin(Request $request)
    {
        // 获取手机好
        $phone = $request->phone;

        // 读取数据库
        $res = Shoper::where('phone',$phone)->first();
        // 店铺id
        $pid = DB::table('shops')->where('spid','=',$res['id'])->value('id');

        // 判断
        if(empty($res)) {
            return back()->with('info','用户名不存在');
        }

        // 检测密码
        if(Hash::check($request->password, $res->password)) {
            // 写入session
            session(['id' => $res['id'],'shopername' => $res['shopername'],'pid'=>$pid]);

            return redirect('/adminShop')->with('info','登陆成功');
        }else{
            return back()->with('info','请重新登陆');
        }
    }

    /**
     * 退出登陆
     */
    public function logout(Request $request)
    {
        $res = session('info');
        $request->session()->flush();

        return redirect('adminShop/shoperLogin') -> with('info',$res);
    }

    
}
